package com.zc.inkdemo.utils;

import android.content.Context;
import android.text.TextUtils;

import com.dianju.showpdf.DJContentView;
import com.zc.inkdemo.Constant;

import java.io.IOException;

public class CommonUtil {
	public static String login(Context context, DJContentView contentView, String userName) {
		int loginRes = -1;
		String res = "fail-";
		if(TextUtils.isEmpty(userName)) {
			res = "fail-空的登录名!";
			return res;
		}
		if(userName.startsWith("HWSEALDEMO")) {//测试用户登陆(不需要授权)
			loginRes = contentView.login(userName, 4, "");
			if(loginRes==1) {//登陆成功
				res = "ok";
			} else {
				res += ("login="+loginRes);
			}
			return res;
		}
		//正式用户登陆(需要授权授权)
		//获取授权模式
		int verifyRes = -1;
		String verifyRes1 = "";
		String authType = ClfUtil.getSPString(context, Constant.AUTHORIZATION_TYPE,  Constant.AUTHORIZATION_TYPE_DEFAULT);
		if("1".equals(authType)) {//按照设备号授权
			//获取授权码
			String verifyLic = ClfUtil.getSPString(context, Constant.AUTHORIZATION_LIC, Constant.AUTHORIZATION_LIC_DEFAULT);
			verifyRes = contentView.verifyLic(verifyLic);
			if(verifyRes!=1) res += ("verifyRes="+verifyRes+";");//授权失败
		} else if("2".equals(authType)) {//账号授权
			try {
				String account1 = ClfUtil.getSPString(context, Constant.AUTHORIZATION_ACCOUNT1, Constant.AUTHORIZATION_ACCOUNT1_DEFAULT);
				verifyRes1 = contentView.getLicOnline("", account1);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				res += e.getMessage();
				return res;
			}//从app存储文件读取
			if(!"ok".equals(verifyRes1)) res += ("verifyRes1="+verifyRes1+";");//授权失败
		} else if("3".equals(authType)) {//二维码授权
			String account2 = ClfUtil.getSPString(context, Constant.AUTHORIZATION_ACCOUNT2, Constant.AUTHORIZATION_ACCOUNT2_DEFAULT);
			verifyRes = contentView.verifyLicById(account2);//从设备读取
			if(verifyRes!=1) res += ("verifyRes="+verifyRes+";");//授权失败
		} else {
			res += "异常的授权模式!";
			return res;
		}
		//调用登陆接口
		loginRes = contentView.login(userName, 2, "");
		if(loginRes!=1) {
			res += ("loginRes="+loginRes);
		}
		//判断此次登陆是否成功了
		if((verifyRes==1 || "ok".equals(verifyRes1)) && loginRes==1){
			res = "ok";
		}
		return res;
	}

}
